package com.atguigu.edu.realtime.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.realtime.constant.EduConfig;
import redis.clients.jedis.Jedis;

import java.util.List;

public class DimUtil {


    public static JSONObject  getDimInfo(String tableName,String id ){
        String querySQL="select * from "+EduConfig.PHOENIX_SCHEMA+"."+tableName+" where id='"+id+"'";

        JSONObject jsonObject = PhoenixUtil.queryOne(querySQL, JSONObject.class);

        return jsonObject;

    }

    public static  JSONObject  getDimInfoWithCache(String tableName,String id ){
        // 1 先查缓存
        Jedis jedis = RedisUtil.getJedis();
        // 1 type? string    2 key ? dim:[tablename]:[id]   3 value ? dim_info_json    4 score/field ? 无
        // 5读取api   get     6 写入api   set
        //  7 超时时间？  1天
        String dimKey="dim:"+tableName+":"+id;

        String dimJson = jedis.get(dimKey);
        JSONObject jsonObject=null;
        if(dimJson!=null &&dimJson.length()>0){
              jsonObject = JSON.parseObject(dimJson);

        }else{ //如果未命中则查询phoenix 并且同步缓存
              jsonObject = getDimInfo(tableName, id);
            //写缓存
            jedis.setex(dimKey,60*60*24 ,jsonObject.toJSONString());

        }
        jedis.close();
        return jsonObject;
    }


    public static void main(String[] args) {
        JSONObject dimInfo = getDimInfo("dim_sku_info", "1");
        System.out.println(dimInfo);
    }


}
